package guanqu.server;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.HistoryIrrBRWaterCannal;
import guanqu.beans.IrrBMSystem;
import guanqu.beans.RealTimeIrrBRWaterCannal;
import manager.PersistentManager;

/**
 * 记录adcp数据
 * @author 凯
 *
 */
public class RecordADCPValue extends HttpServlet {
	@Override
	public void init() throws ServletException {
		super.init();
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		// Session session = sf.openSession();
		Session session = sf.getCurrentSession();

		UpLoad upLoad = new UpLoad();

		Transaction tx = session.beginTransaction();
		try {

			String data = req.getParameter("data");

			// Download download = new Download();

			//
			// AdcpUpLoad adcpUpload = JSON.parse(input, features);

			HistoryIrrBRWaterCannal historyIrrWaterChannel = new HistoryIrrBRWaterCannal();

			String strID = req.getParameter("adcp.id");
			historyIrrWaterChannel.setStadiaCode(strID);

			// String strName = req.getParameter("adcp.name");
			// historyIrrWaterChannel.se(strName);

			BigDecimal bRate = new BigDecimal(req.getParameter("adcp.rate"));
			historyIrrWaterChannel.setFlux(bRate);

			BigDecimal bWaterLevel = new BigDecimal(req.getParameter("adcp.Water_level"));
			historyIrrWaterChannel.setWaterHeight(bWaterLevel);

			String strTime = req.getParameter("adcp.time");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = null;
			try {
				date = (Date) sdf.parse(strTime);
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			Calendar calTime = Calendar.getInstance();
			calTime.setTime(date);
			historyIrrWaterChannel.setMeaTime(calTime);

			if (req.getParameter("adcp.method").equals(String.valueOf('p'))) {
				Character cMethod = req.getParameter("adcp.method").charAt(0);
				historyIrrWaterChannel.setMeaFluxMethod(cMethod);
			} else {

			}

			// 获取实时渠道信息
			session.save(historyIrrWaterChannel);

			tx.commit();
			upLoad.setStatus(DownloadInfo.SUCCESS);
		} catch (Exception e) {
			upLoad.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		} finally {
			// session.close();
		}

		System.out.println(JSON.toJSONString(upLoad));
		resp.setCharacterEncoding("UTF-8");
		resp.getWriter().append(JSON.toJSONString(upLoad));
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

	public static class UpLoad {
		int status;

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

	}
}
